
package com.study.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IAddresdetailDAO;
import com.study.model.jdao.IAffixDAO;
import com.study.model.jdao.ICollectionDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.AnniversaryMapper;
import com.study.model.mdao.CareDAO;
import com.study.model.mdao.CompetitorsMapper;
import com.study.model.mdao.CusmshfitMapper;
import com.study.model.mdao.CustomerMapper;
import com.study.model.mdao.DeliveryscheduleMapper;
import com.study.model.mdao.DemantMapper;
import com.study.model.mdao.LinkmanMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.MoneybackplanMapper;
import com.study.model.mdao.OffersDAO;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.mdao.ReturnmoreMapper;
import com.study.model.mdao.ServiceDAO;
import com.study.model.mdao.SolutionMapper;
import com.study.model.mdao.TousuDAO;
import com.study.model.mdao.WeixiuDAO;
import com.study.model.pojo.Addresdetail;
import com.study.model.pojo.Affix;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.User;
import com.study.util.CustormAll;

@Service
@Transactional(rollbackOn = Exception.class)
public class CustomerService {
	@Autowired
	ICustomerDAO dao;
	@Autowired
	IUserDAO userDAO;
	@Autowired
	CustomerMapper mapper;
	@Autowired
	CustormAll custormall;
	@Autowired
	LinkmanMapper linkmanMapper;
	@Autowired
	IDictionaryDAO iDictionaryDAO;

	@Autowired
	IAddresdetailDAO iAddresdetailDAO;

	@Autowired
	IAffixDAO iAffixDAO;

	@Autowired
	LogforcustDAO logforcustDAO;

	@Autowired
	AnniversaryMapper annMapper;
	@Autowired
	CareDAO careMapper;
	@Autowired
	OpportunityMapper oppMapper;
	@Autowired
	DemantMapper demantMapper;
	@Autowired
	SolutionMapper solutionMapper;
	@Autowired
	CompetitorsMapper competitorsMapper;
	@Autowired
	OffersDAO offersMapper;
	@Autowired
	OrderFormMapper  orderMapper;
	@Autowired
	MoneybackplanMapper moneyBackPlanMapper;
	@Autowired
	DeliveryscheduleMapper deliveryMapper;
	@Autowired
	ReturnmoreMapper returnMapper;
	@Autowired
	ServiceDAO serviceMapper;
	@Autowired
	TousuDAO tousuMapper;
	@Autowired
	WeixiuDAO weixiuMapper;

	/**
	 * Wcx 根据用户id查询可见用户
	 * 
	 * @return
	 */
	public Iterable<Customer> findAll() {
		return dao.findAll();
	}

	/**
	 * 修改客户状态
	 * 
	 * @return
	 */
	public boolean updateCust(String xms) {
		System.out.println(xms + "      xms");
		Customer findId = dao.findByCusName(xms);
		findId.setCusShow(0);

		Customer save = dao.save(findId);
		return !save.equals("") ? true : false;
	}

	/**
	 * 查询自己可见的客户
	 * 
	 * @param cusshow
	 * @param user
	 * @return
	 */
	public List<Customer> getFindByCusShowAndUser(int cusshow, int user) {
		User finduseid = userDAO.findByUseId(user);
		List<Customer> finduser = dao.findByCusShowAndUser(cusshow, finduseid);
		return finduser;
	}

	/**
	 * 根据用户id查询所有可合并的客户记录
	 * 
	 * @param user
	 * @return
	 */
	public List<JSONObject> getFindCusmtionUser(String user) {
		List<JSONObject> selectnotcustmoer = null;
		if (user == null) {
			selectnotcustmoer = mapper.selectnotcustmoer(0);
		} else {
			selectnotcustmoer = mapper.selectnotcustmoer(Integer.parseInt(user));
		}

		return selectnotcustmoer;
	}

	/**
	 * 分页查询 Wcx
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Customer> selectByPager(int pageNum, int pageSize, int uid) {
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		User user = userDAO.findByUseId(uid);
		// 获取
		List<Customer> list = dao.findByCusShowAndUser(1, user);
		System.out.println("大小" + list.size());
		return list;
	}

	/**
	 * 带条件的分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(String name, String selects, String useid) {
		String state = null;
		String credit = null;

		if (selects.length() > 0 && selects.startsWith("113")) {
			credit = selects;
		} else if (selects.length() > 0 && selects.startsWith("112")) {

			state = selects;
			System.out.println("进来" + state);
		}
		String input = "%" + name + "%";
		List<JSONObject> selectByWhere = new ArrayList<JSONObject>();
		if ("6".equals(useid)) {
			selectByWhere = mapper.selectByWhere(credit, state, input);
		} else {
			selectByWhere = mapper.selectByWhereUse(credit, state, input, useid);
		}

		return selectByWhere;
	}

	/**
	 * Wcx 添加一条客户信息
	 */
	public boolean addCustomer(Customer cus) {
		boolean flag = false;
		try {
			dao.save(cus);
			flag = true;
		} catch (Exception e) {
			System.out.println("adsadsads");
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * Wcx 添加客户信息(mapper)
	 * 
	 * @param names
	 * @param src
	 * @param high 
	 * @return
	 */
	public boolean addcustomer(String namese, String src, String custer, String famoney, String state, String region,
			String privoder, String regionxiao, String dignxing, String dignji, String textarea4, String useid, Integer high) {
		
		
		return mapper.addCustomer1(namese, src, custer,"1121", region, privoder, regionxiao, dignxing, dignji,
				textarea4, useid, 1, new Date(),high);

	}

	/**
	 * 查询所有可见状态的客户 Wcx
	 * 
	 * @return
	 */
	public List<Customer> findlook(int uid) {
		List<Customer> cusShowAndUser = new ArrayList<Customer>();
		try {
			User user = userDAO.findByUseId(uid);
			System.out.println(user.getUseName());
			cusShowAndUser = dao.findByCusShowAndUser(1, user);
			System.out.println(cusShowAndUser.size() + "长度");
			for (Customer customer : cusShowAndUser) {
				System.out.println("dasdasds" + customer.getCusName());

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cusShowAndUser;

	}

	/**
	 * 删除客户
	 * 
	 * @param cusid
	 * @return
	 */
	public boolean deleteCustomer(int[] cusids) {
		boolean flag = false;

		try {
			for (int cusid : cusids) {
				Customer cus = dao.findByCusId(cusid);
				cus.setCusShow(0);
				dao.save(cus);
			}
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 查询所有可见状态的分种类客户 Wcx
	 * 
	 * @return
	 */
	public List<Customer> findlook(int uid, int custype) {
		List<Customer> cusShowAndUser = new ArrayList<Customer>();
		try {
			User user = userDAO.findByUseId(uid);
			System.out.println(user.getUseName());
			cusShowAndUser = dao.findByCusShowAndUser(1, user);
			System.out.println(cusShowAndUser.size() + "长度");
			for (Customer customer : cusShowAndUser) {
				System.out.println("dasdasds" + customer.getCusName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cusShowAndUser;
	}

	/**
	 * 根据id查询用户
	 * 
	 * @param cusId
	 * @return
	 */
	public Customer findById(int cusId) {
		return dao.findByCusId(cusId);
	}

	public List<JSONObject> selectAll() {
		return mapper.selectAll(0,"aaa");
	}

	/**
	 * 根据id查询用户(mapper)
	 */
	public JSONObject selectBycusid(int cusid) {
		return mapper.selectBycusid(cusid);
	}

	/**
	 * 修改客户
	 */

	public boolean update(String cusid, String namese, String custer, String phone, String state, String region,
			String privoder, String regionxiao, String dignxing, String dignji, String textarea4, String useid,
			String img) {
		boolean flag = false;
		try {
			Customer customer = dao.findByCusId(Integer.parseInt(cusid));
			customer.setCusName(custer);
			customer.setCusCompany(namese);
			customer.setCusimg(img);
			Dictionary dictionary2 = iDictionaryDAO.findByDicId(Integer.parseInt(state));
			customer.setDictionary2(dictionary2);
			Dictionary dictionary3 = iDictionaryDAO.findByDicId(Integer.parseInt(region));
			customer.setDictionary3(dictionary3);
			Dictionary dictionary4 = iDictionaryDAO.findByDicId(Integer.parseInt(privoder));
			customer.setDictionary4(dictionary4);
			Dictionary dictionary1 = iDictionaryDAO.findByDicId(Integer.parseInt(regionxiao));
			customer.setDictionary1(dictionary1);
			Dictionary dictionary5 = iDictionaryDAO.findByDicId(Integer.parseInt(dignxing));
			customer.setDictionary5(dictionary5);
			Dictionary dictionary6 = iDictionaryDAO.findByDicId(Integer.parseInt(dignji));
			customer.setDictionary6(dictionary6);
			Affix affix = iAffixDAO.findByAffixId(1);
			customer.setAffix(affix);
			Addresdetail addresdetail = iAddresdetailDAO.findByAdId(1);
			customer.setAddresdetail(addresdetail);
			customer.setCusRemark(textarea4);
			dao.save(customer);
			String meg = null;

			String logMessage = "修改了客户信息";
			logforcustDAO.insert(Integer.parseInt(cusid), new Date(), Integer.parseInt(useid), 2204, logMessage, "");
			flag = true;
		} catch (Exception e) {
			System.out.println(e);
		}

		return flag;
	}

	/**
	 * 通过名字找到客户
	 */
	public Customer selectByName(String name) {
		return dao.findByCusName(name);
	}

	/**
	 * 查询指定id客户信息
	 * 
	 * @param ids
	 * @return
	 */
	public Customer findCustomer(int ids) {
		return dao.findByCusId(ids);
	}

	/**
	 * 返回对应名称客户所有联系人
	 * 
	 * @param names
	 * @return
	 */
	public List<Linkman> selectById(Integer names) {
		Customer selectByName = mapper.selectByIdsAll(names);

		System.out.println(selectByName);
		List<Linkman> findAll = custormall.findAll(selectByName.getCusId(), 1);
		for (Linkman customer : findAll) {
			System.out.println(customer + "出来了");
		}
		return findAll;
	}

	/**
	 * 返回对应名称单个客户信息
	 * 
	 * @param names
	 * @return
	 */
	public Customer selectCustomerByName(String names) {
		return mapper.selectByName(names);
	}

	public List<JSONObject> getfindCustName(String custname) {
		return mapper.selectCustName(custname);
	}

	/**
	 * 客户视图
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject queryCus(String id) {
		return mapper.selectId(id);
	}

	/**
	 * 查询时间轴
	 * 
	 * @param id
	 * @param type 
	 */
	public List<JSONObject> selectBytime(String id, Integer type) {
		return logforcustDAO.selectByWhere(id,type);
	}

	public JSONObject selectBywhere(String namese, String custer, String textarea4) {
		return mapper.selectbycompanyAndremark(namese, custer, textarea4);

	}

	/**
	 * 查看公海数据
	 * 
	 * @return
	 */
	public List<JSONObject> getgh() {

		return mapper.selectgh();
	}

	/**
	 * 模糊查询客户
	 * 
	 * @param msg
	 * @return
	 */
	public List<JSONObject> findByLike(String msg) {
		if (!"".equals(msg) && msg != null) {
			System.out.println("我要模糊查询客户。");
			return mapper.selectByLike("%" + msg + "%");
		} else {
			return null;
		}
	}

	/**
	 * 根据电话查询客户
	 * 
	 * @param msg
	 * @return
	 */
	public List<JSONObject> findByPhone(String msg) {
		// TODO Auto-generated method stub
		return mapper.selectByPhone(msg);
	}

	public int updatestate(String custid) {

		return mapper.updatestate(custid);
	}

	/**
	 * 查看当前客户的状态
	 * 
	 * @param custid
	 * @return
	 */
	public JSONObject selectStatus(String custid) {

		return mapper.selectStatus(custid);
	}
	
	/**
	 * 查询各个阶段的总数
	 * @param id 
	 * @return
	 */
	public JSONObject findJilu(String id) {
		JSONObject object = new JSONObject();
		//纪念日
		object.put("annCountView", annMapper.findCountById(id));
		//关怀
		object.put("careCountView", careMapper.findCountById(id));
		//销售机会
		object.put("oppCountView", oppMapper.findCountById(id));
		//客户需求
		object.put("demandCountView", demantMapper.findCountById(id));
		//解决方案
		object.put("suloCountView", solutionMapper.findCountById(id));
		//竞争对手
		object.put("compeCountView", competitorsMapper.findCountById(id));
		//报价
		object.put("offerCountView", offersMapper.findCountById(id));
		//订单
		object.put("orderCountView", orderMapper.findCountById(id));
		//发货
		object.put("deliCountView", deliveryMapper.findCountById(id));
		//退货
		object.put("backCountView", returnMapper.findCountById(id));
		//服务
		object.put("serviceCountView", serviceMapper.findCountById(id));
		//投诉
		object.put("tousuCountView", tousuMapper.findCountById(id));
		//维修
		object.put("weixiuCountView", weixiuMapper.findCountById(id));
		
		return object;

	}
	
	/**
	 * 查询流水表的数量
	 */
	public List<JSONObject> findTimeByCount(String cusid){
		return logforcustDAO.findTimeByCount(cusid);
	}
    /**
     * 修改三一客
     * @param id
     * @param qualitative
     * @param grading
     * @param ration
     * @return
     */
	public boolean updatethree(String id, String qualitative, String grading, String ration,String useid) {
		// TODO Auto-generated method stub
		boolean flag= mapper.updatethree(id,qualitative,grading,ration);
		 String logMessage = "修改了三定一节点信息";
			logforcustDAO.insert(Integer.parseInt(id), new Date(), Integer.parseInt(useid
					), 2201, logMessage, "");
		return flag;
	}
    /**
     * 查看三定一节点
     * @param id
     * @return
     */
	public JSONObject selectthree(String id) {
		
		return mapper.selectthree(id);
	}
}
